$(function () {
    getUserInfo();
    const layer = layui.layer;
    $("#btnLoginout").on('click', function () {
        layer.confirm('是否确认退出?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            localStorage.removeItem('token');
            location.href = "./login.html";
            layer.close(index);
        });
    })
})

function getUserInfo() {
    $.ajax({
        type: "GET",
        url: "/my/userinfo",
        // headers: {
        //     Authorization: localStorage.getItem("token") || "[]"
        // },
        success: function (res) {
            if (res.status !== 0) {
                return layui.layer.msg('请求失败！')
            }
            renderAvatar(res.data);
            console.log(res.data);
        }
    })
}

function renderAvatar(user) {
    const name = user.nickname || user.username;
    $('.welcome').html("欢迎" + name);
    if (user.user_pic !== null) {
        $('.layui-nav-img').attr("src", user.user_pic);
        $('.layui-nav-img').show();
        $(".text-avatar").hide();
    } else {
        first = name[0].toUpperCase();
        $(".text-avatar").html(first);
        $('.layui-nav-img').hide();
        $(".text-avatar").show();
    }
}